<?php


namespace app\service;

use think\facade\Db;
use app\model\ModelReceivable;
use think\response\Json;

/**
 * 支付服务类
 * Class PaymentService
 * @package app\service
 */
class PaymentService extends BaseService
{
    /**
     * 随机获取可用支付的卡信息
     * @param int type 支付卡类型
     * @param string field 查询的字段，因为类型不同用的字段不同
     * @return array
     */
    public function getCardInfoByType(int $type,string $field='') : array
    {
        if (empty($type)) {
            return $this->error('参数错误');
        }
        if ($type == 101 || $type == 201 || $type == 301) {
            $modelReceivable = new ModelReceivable();
            $where = [
                ['type', '=', $type],
                ['status', '=', 1],
                ['current_receive_amount', '<=', 'amount_limit']
            ];
            if (empty($field)) {
                $field = '*';
            }
            $result = $modelReceivable
                ->where($where)
                ->whereRaw('rand()')
                ->field($field)
                ->findOrEmpty()
                ->toArray();
            //echo $modelReceivable->getLastSql(); die();
            return $this->success('成功', $result);
        } else {
            return $this->error('卡类型错误');
        }
    }

    /**
     * 获取VIP对应的金额费用
     * @param int $lv VIP级别
     * @return array
     */
    public function getVipFeeByVipLv(int $lv): array
    {
        if (empty($lv)) {
            return $this->error('非法访问');
        }
        $fee = 0;
        switch ($lv) {
            case 1:
                $fee = 288;
                break;
            case 2:
                $fee = 788;
                break;
            case 3:
                $fee = 1988;
                break;
            case 4:
                $fee = 3888;
                break;
            case 5:
                $fee = 5888;
                break;
            case 6:
                $fee = 8888;
                break;
            case 7:
                $fee = 12888;
                break;
        }
        return $this->success('成功', $fee);
    }
}